#include "pushbutton.h"

PushButton::PushButton(QWidget *parent)
  :QPushButton(parent)
{
  status = NORMAL;
  isPressed = false;
}

PushButton::~PushButton()
{

}

void PushButton::loadPixmap(QString pic_name)
{
  pixmap.load(pic_name);
  //QSize picSize(96, 32);
  pixmap = pixmap.scaledToHeight(20);
  btn_width = pixmap.width()/3;
  btn_height = pixmap.height();
  setMinimumSize(btn_width, btn_height);
}

void PushButton::enterEvent(QEvent *)
{
  status = ENTER;
  update();
}

void PushButton::mousePressEvent(QMouseEvent *event)
{
  if (event->button() == Qt::LeftButton)
  {
    isPressed = true;
    status = PRESS;
    update();
  }
}

void PushButton::mouseReleaseEvent(QMouseEvent *)
{
  if(isPressed)
  {
    isPressed = false;
    status = ENTER;
    update();
    emit clicked();
  }
}

void PushButton::leaveEvent(QEvent *)
{
  status = NORMAL;
  update();
}

void PushButton::paintEvent(QPaintEvent *)
{
  QPainter painter(this);
  painter.drawPixmap(rect(), pixmap.copy(btn_width * status, 0, btn_width, btn_height));
}
